import { createApp } from "vue";
import "./styles/index.scss";
import App from "./App.vue";
import elementPlus from "element-plus";
import router from "./router";
import BaseTable from "./components/base-table/index.vue";
import BaseForm from "./components/base-form/index.vue";
import BaseTree from "./components/base-tree.vue";
import EntityManager from "./components/entity-manager/index.vue";
import TitleBar from "./components/title-bar.vue";
import { createPinia } from "pinia";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import { startWebSocket } from "@/utils/ws.js";
import { useSysStore } from "./store";
import * as loginApis from "@/apis/login";
import * as sysApis from "@/apis/sys";
import { directives } from "./utils/directives";
import * as utils from "./utils/utils";
import https from "./utils/https";
import { ElMessage, ElMessageBox } from "element-plus";

const app = createApp(App);

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}

const pinia = createPinia();
app.component("BaseForm", BaseForm)
    .component("BaseTable", BaseTable)
    .component("EntityManager", EntityManager)
    .component("titleBar", TitleBar)
    .component("baseTree", BaseTree)
    .use(elementPlus)
    .use(pinia)
    .use(directives);

const apis = {
    https,
    utils,
    success: (msg) => ElMessage.success(msg || "操作成功"),
    error: ElMessage.error,
    confirm: ElMessageBox.confirm,
    prompt: ElMessageBox.prompt,
};

window.app = apis;

app._context.components.ElDialog.props.closeOnClickModal.default = false;
app._context.components.ElDialog.props.destroyOnClose.default = true;
app._context.components.ElDrawer.props.closeOnClickModal.default = false;
app._context.components.ElDrawer.props.destroyOnClose.default = true;

// 获取初始化信息
sysApis
    .getInitInfo()
    .then((resp) => {
        // 初始化所有应用路由信息
        const sysStore = useSysStore();
        sysStore.init(resp);

        // 启动websocket
        window.websocket = startWebSocket();

        app.use(router);
        app.mount("#app");
        window.app.tagTabsVisible = sysStore.tagTabsVisible();
    })
    .catch(() => {
        // 启动websocket
        window.websocket = startWebSocket();

        app.use(elementPlus).use(router).mount("#app");
    });
